//
//  OtherController.swift
//  DaRings
//
//  Created by 易先生 on 2022/10/28.
//

import UIKit
import MYModule
import RxCocoa
import RxSwift
import RxDataSources

class OtherController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.backgroundColor =  UIColor.darkBackground
        
        view.backgroundColor =  UIColor.darkBackground
        view.addSubview(titleView)
        view.addSubview(tableView)
        
        titleView.snp.makeConstraints { make in
            make.top.equalTo(topLayoutGuide.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom)
            make.leading.trailing.bottom.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        let dataSource = RxTableViewSectionedReloadDataSource<OtherSectionModel> { (dataSource, tableView, indexPath, _) in
            
            let item = dataSource[indexPath]
            
            switch item {
            case .unitFormatItem(cellModel: let cellModel),
                    .maximumHeartRateItem(cellModel: let cellModel):
                let cell = tableView.dequeueReusableCell(withIdentifier: OtherTimeFormatCell.reuseIdentifier) as! OtherTimeFormatCell
                cell.bind(to: cellModel)
                if cellModel.title == "measurement_system".language {
                    cell.setRoundedCorners(.top, radius: 18)
                } else if cellModel.title == "device_heart_rate_remind_max".language {
                    cell.setRoundedCorners(.bottom, radius: 18)
                } else {
                    cell.setRoundedCorners(.all, radius: 0)
                }
                return cell
            case .sedentaryReminderItem(cellModel: let cellModel),
                    .lowBatteryReminderItem(cellModel: let cellModel),
                    .heartRateWarningItem(cellModel: let cellModel):
                let cell = tableView.dequeueReusableCell(withIdentifier: OtherSedentaryReminderCell.reuseIdentifier) as! OtherSedentaryReminderCell
                
                cell.titleLabel.font = Fonts.systemFont(ofSize: 22)
                
                cell.bind(to: cellModel)
                if cellModel.title == "device_heart_rate_remind_title".language {
                    cell.titleLabel.font = Fonts.boldSystemFont(ofSize: 22)
                    cell.setRoundedCorners(.top, radius: 18)
                }else if cellModel.title == "other_low_battery_reminder_title".language{
                    cell.setRoundedCorners(.bottom, radius: 18)
                }else{
                    cell.setRoundedCorners(.all, radius: 0)
                }
                return cell
            }
        }
        viewModel.sections.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: rx.disposeBag)
        tableView.rx.setDelegate(self).disposed(by: rx.disposeBag)
        
        tableView.rx.itemSelected.subscribe(onNext: { [weak self] indexPath in
            guard let self = self else { return }
            let item = self.viewModel.sections.value[indexPath.section].items[indexPath.row]
            switch item {
            case .unitFormatItem(cellModel: let cellModel):

                let vc = ProfilePopController(showType: .until)
                vc.itemValueChange.subscribe {_ in
                    
                    let profile = UserCacheManagement.userInfo()
                    
                    cellModel.value.accept(profile.unitType.localeName)
                }.disposed(by: self.rx.disposeBag)
                self.present(vc, animated: true)
            case .maximumHeartRateItem(cellModel: let cellModel):
                let vc = ProfilePopController(showType: .heartMax)
                vc.itemValueChange.subscribe {_ in
                    let profile = UserCacheManagement.userInfo()
                    self.viewModel.sendHeartRateRemind(isRemind: profile.heartRemind, max: profile.warningMxHR)
                    cellModel.value.accept(profile.getMaxHR())
                }.disposed(by: self.rx.disposeBag)
                self.present(vc, animated: true)
            default:
                break
            }
        }).disposed(by: rx.disposeBag)
    }
    
    private lazy var viewModel: OtherViewModel = {
        let viewModel = OtherViewModel()
        return viewModel
    }()

    private lazy var titleView: TopTitleView = {
        let titleView = TopTitleView()
        titleView.backgroundColor =  UIColor.darkBackground
        titleView.configTitle(title: "other_settings_title".language)
        return titleView
    }()
    
    private lazy var tableView: TableView = {
        let tableView = TableView(frame: .zero, style: .plain)
        tableView.separatorColor = .clear
        tableView.backgroundColor = .clear
        tableView.sectionFooterHeight = 0
        tableView.sectionHeaderHeight = 20
        tableView.register(OtherTimeFormatCell.self, forCellReuseIdentifier: OtherTimeFormatCell.reuseIdentifier)
        tableView.register(OtherSedentaryReminderCell.self, forCellReuseIdentifier: OtherSedentaryReminderCell.reuseIdentifier)
        return tableView
    }()
}

extension OtherController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 20))
        return sectionView
    }
}
